<?php

use Illuminate\Database\Capsule\Manager as DB;

class IndexController extends AbstractController
{
    //构造函数
    public function init(){
//        $session=Yaf_Session::getInstance();
//        if( $session -> islogin != 1 )
//        {
//            success("请先登录","Admin/Login/index");
//        }

    }
    // 默认Action
    public function indexAction()
    {

        $this->getView()->display('common/header.html');
        $this->getView()->display('index/index.html');
        $this->getView()->display('common/footer.html');

    }
###################################问卷模块 start################################
    // 问卷列表
    public function questionnaire_listAction()
    {
        $where = [];
        $que_list = DB::table('questionnaire')
            ->where($where)
            ->orderBy('qu_order','desc')
            ->paginate(20);

        $this->getView()->assign('que_list',$que_list);
        $this->getView()->display('common/header.html');
        $this->getView()->display('index/questionnaire_list.html');
        $this->getView()->display('common/footer.html');
    }

    //添加问卷
    public function questionnaire_addAction()
    {
        //判断是否有表单提交
        if ($this->getRequest()->getPost())
        {

            $data['qu_title'] = $this->getRequest()->getPost('qu_title','');
            $data['qu_description'] = $this->getRequest()->getPost('qu_description','');
            $data['qu_order'] = $this->getRequest()->getPost('qu_order','0');
            $data['qu_status'] = $this->getRequest()->getPost('qu_status','');
            $data['qu_startage'] = $this->getRequest()->getPost('qu_startage','');
            $data['qu_endage'] = $this->getRequest()->getPost('qu_endage','');
            $data['qu_addtime'] = time();

            $res = DB::table('questionnaire')->insert($data);
            if( $res )
            {
                success('添加成功','Admin/Index/questionnaire_list');
            } else {
                error('添加失败');
            }
        }
        else
        {

            $this->getView()->display('common/header.html');
            $this->getView()->display('index/questionnaire_add.html');
            $this->getView()->display('common/footer.html');

        }
    }

    //删除问卷
    public function questionnaire_delAction()
    {
        $data['qu_id'] = $this->getRequest()->getQuery('qu_id');

        $res = DB::table('questionnaire')->where($data)->delete();

        if( $res )
        {
            success('删除成功','Admin/Index/questionnaire_list');
        } else {
            error('删除失败');
        }
    }

    //修改问卷
    public function questionnaire_editAction()
    {
        //判断是否有表单提交
        if ($this->getRequest()->getPost())
        {

            $data['qu_title'] = $this->getRequest()->getPost('qu_title','');
            $data['qu_description'] = $this->getRequest()->getPost('qu_description','');
            $data['qu_endage'] = $this->getRequest()->getPost('qu_endage','');
            $data['qu_deviation'] = $this->getRequest()->getPost('qu_deviation','');
            $data['qu_mean'] = $this->getRequest()->getPost('qu_mean','');
            $data['qu_status'] = $this->getRequest()->getPost('qu_status','');
            $data['qu_startage'] = $this->getRequest()->getPost('qu_startage','');
            $data['qu_endage'] = $this->getRequest()->getPost('qu_endage','');
            $where['qu_id'] = $this->getRequest()->getPost('qu_id');
            $res = DB::table('questionnaire')->where($where)->update($data);
            if( $res )
            {
                success('修改成功','Admin/Index/questionnaire_list');
            } else {
                error('修改失败');
            }

        } else {

            //问卷详细信息
            $where['qu_id'] = $this->getRequest()->getQuery('qu_id');
            $que_info = DB::table('questionnaire')->where($where)->first();

            $this->getView()->assign('que_info',$que_info);

            //题目相关维度
            $whereDIM['dim_questionid'] = $where['qu_id'];
            $whereDIM['dim_father'] = 0 ;
            $whereDIM['dim_status'] = 1 ;
            $dim_list = DB::table('dimension')->where($whereDIM)->orderBy('dim_addtime','asc')->get();

            //查询维度所属的小维度
            foreach( $dim_list as $key => $val )
            {
                $dim_list[$key]['children'] = DB::table('dimension')->where('dim_father','=',$val['dim_id'])->orderBy('dim_addtime','asc')->get();
            }

        //输出方式
            //查询该问卷所有维度 然后他在成绩计算里雷达图下拉时显示的数量
            //该问卷所有维度
            $dim_radar_list = DB::table('dimension')->where("dim_questionid","=",$where['qu_id'])->get();

            //显示在雷达图的维度
            $dim_radar_is = DB::table('que_radar_dimension')
                ->leftJoin("dimension","dim_id","=","qrd_dimensionid")
                ->where("qrd_questionnaireid","=",$where['qu_id'])
                ->where("qrd_dimensionid","!=",0)
                ->orderBy("qrd_addtime","asc")
                ->get();

//var_dump($dim_radar_is);exit;
            $dim_count = count($dim_radar_list) - count($dim_radar_is);
            $this->getView()->assign('dim_list',$dim_list);
            $this->getView()->assign('dim_count',$dim_count);
            $this->getView()->assign('dim_radar_list',$dim_radar_list);
            $this->getView()->assign('dim_radar_is',$dim_radar_is);

            $this->getView()->display('common/header.html');
            $this->getView()->display('index/questionnaire_edit.html');
            $this->getView()->display('common/footer.html');

        }

    }

    //修改问卷雷达图分数上限
    public function questionnaire_edit_radar_numberAction()
    {
        $where['qu_id'] = $this->getRequest()->getQuery('qu_id');
        $data['qu_radar_number'] = $this->getRequest()->getQuery('number');
        
        $res = DB::table("questionnaire")->where($where)->update($data);
        if( $res )
        {
            echo 1;
        } else {
            echo 2;
        }
    }
###################################问卷模块 end################################
###################################题目模块 start################################
    // 题目列表
    public function topic_listAction()
    {

        $where['top_status'] = 1;
        $qu_id = 0;
        $session=Yaf_Session::getInstance();
        if( $session->qu_id ){
            $qu_id = $session->qu_id ;
            $where['top_questionnaireid'] = $session->qu_id ;
        }

        //如果有筛选
        if( $this->getRequest()->getQuery('qu_id') )
        {

            $session->qu_id = $this->getRequest()->getQuery('qu_id');
            $qu_id = $session->qu_id ;
//            var_dump($session->qu_id);exit;
            $where['top_questionnaireid'] = $this->getRequest()->getQuery('qu_id') ;
        }
        if( $this->getRequest()->getQuery('page'))
        {
            $page = $this->getRequest()->getQuery('page') ;
        } else {
            $page = 1 ;
        }

        $topic_list = DB::table('topic')
            ->join('questionnaire','top_questionnaireid','=','qu_id')
            ->where($where)
            ->orderBy('top_order','desc')
            ->paginate(20 );

        //问卷列表
        $whereQU['qu_status'] = 1;
        $que_list = DB::table('questionnaire')->where($whereQU)->get();

        $this->getView()->assign('que_list',$que_list);
        $this->getView()->assign('topic_list',$topic_list);
        $this->getView()->assign('qu_id',$qu_id);

        $this->getView()->display('common/header.html');
        $this->getView()->display('index/topic_list.html');
        $this->getView()->display('common/footer.html');
    }

    //添加题目
    public function topic_addAction()
    {
        //判断是否有表单提交
        if ($this->getRequest()->getPost())
        {
            $data['top_questionnaireid'] = $this->getRequest()->getPost('top_questionnaireid');
            $data['top_dimension'] = $this->getRequest()->getPost('top_dimension');
            $data['top_section'] = $this->getRequest()->getPost('top_section');
            $data['top_title'] = $this->getRequest()->getPost('top_title');
            $data['top_type'] = $this->getRequest()->getPost('top_type');
            $data['top_second_dimension'] = $this->getRequest()->getPost('top_second_dimension');
            $data['top_type'] = $this->getRequest()->getPost('top_type');
            $data['top_order'] = $this->getRequest()->getPost('top_order');
            $data['top_status'] = $this->getRequest()->getPost('top_status');
            $data['top_left_text'] = $this->getRequest()->getPost('top_left_text');
            $data['top_right_text'] = $this->getRequest()->getPost('top_right_text');
            $data['top_interval'] = $this->getRequest()->getPost('top_interval');
            $data['top_reverse'] = $this->getRequest()->getPost('top_reverse')==1?'1':'2';
            $data['top_addtime'] = time();
//            var_dump($this->getRequest()->getPost('top_minnumber'));exit;

            //如果是量表题
            if( $data['top_type'] == 4 )
            {
                $data['top_minnumber'] = $this->getRequest()->getPost('top_minnumber');
                $data['top_maxnumber'] = $this->getRequest()->getPost('top_maxnumber');
            }

            $res = DB::table('topic')->insertGetId($data);
            if( $res )
            {
                //如果是单选题或多选题
                if ( $data['top_type'] == 1 || $data['top_type'] == 2 )
                {
                    $topic_option = $this->getRequest()->getPost('option');
                    $option_guage = $this->getRequest()->getPost('option_grade');
                    foreach ( $topic_option as $key => $val )
                    {
                        if( $val != '' ) {
                            $option_data['opt_topicid'] = $res;
                            $option_data['opt_desc'] = $val ;
                            $option_data['opt_grade'] = $option_guage[$key];
                            $option_data['opt_addtime'] = time();
                            DB::table('topic_option')->insertGetId($option_data);
                        }
                    }
                } else if( $data['top_type'] == 5 ) {
                    //如果是跳转题
                    $topic_option = $this->getRequest()->getPost('option_guage');
                    $guage_id = $this->getRequest()->getPost('guage_id');
//                    var_dump($topic_option);exit;
                    foreach ( $topic_option as $key => $val )
                    {
                        $opt_guageid = $guage_id[$key];
                        if( $val != '' ) {
                            $option_data['opt_topicid'] = $res;
                            $option_data['opt_desc'] = $val;
                            $option_data['opt_guageid'] = $opt_guageid;
                            $option_data['opt_topic_type'] = 5;
                            $option_data['opt_addtime'] = time();
                            DB::table('topic_option')->insertGetId($option_data);
                        }
                    }
                }

                success('添加成功','Admin/Index/topic_list');

            } else {
                error('添加失败');
            }

        } else {

            //问卷列表
            $where['qu_status'] = 1;
            $que_list = DB::table('questionnaire')->where($where)->get();

            $this->getView()->assign('que_lsit',$que_list);

            $this->getView()->display('common/header.html');
            $this->getView()->display('index/topic_add.html');
            $this->getView()->display('common/footer.html');

        }
    }


    //删除题目
    public function topic_delAction()
    {
        $data['top_id'] = $this->getRequest()->getQuery('top_id');

        $res = DB::table('topic')->where($data)->delete();

        if( $res )
        {
            success('删除成功','Admin/Index/topic_list');
        } else {
            error('删除失败');
        }
    }

    //修改题目
    public function topic_editAction()
    {

        //判断是否有表单提交
        if ($this->getRequest()->getPost())
        {
            $top_id = $this->getRequest()->getPost('top_id');
            $data['top_questionnaireid'] = $this->getRequest()->getPost('top_questionnaireid');
            $data['top_dimension'] = $this->getRequest()->getPost('top_dimension');
            $data['top_section'] = $this->getRequest()->getPost('top_section');
            $data['top_title'] = $this->getRequest()->getPost('top_title');
            $data['top_type'] = $this->getRequest()->getPost('top_type');
            $data['top_second_dimension'] = $this->getRequest()->getPost('top_second_dimension');
            $data['top_type'] = $this->getRequest()->getPost('top_type');
            $data['top_order'] = $this->getRequest()->getPost('top_order');
            $data['top_status'] = $this->getRequest()->getPost('top_status');
            $data['top_left_text'] = $this->getRequest()->getPost('top_left_text');
            $data['top_right_text'] = $this->getRequest()->getPost('top_right_text');
            $data['top_interval'] = $this->getRequest()->getPost('top_interval');
            $data['top_reverse'] = $this->getRequest()->getPost('top_reverse')==1?'1':'2';

            $data['top_addtime'] = time();
//            var_dump($this->getRequest()->getPost('top_minnumber'));exit;
            if( $data['top_type'] == 4 )
            {
                $data['top_minnumber'] = $this->getRequest()->getPost('top_minnumber');
                $data['top_maxnumber'] = $this->getRequest()->getPost('top_maxnumber');
            }

            //修改题目
            $res = DB::table('topic')->where('top_id','=',$top_id)->update($data);
            if( $res )
            {
                //如果题目类型是单选或多选
                if ( $data['top_type'] == 1 || $data['top_type'] == 2)
                {
                    $topic_option = $this->getRequest()->getPost('option');
                    $option_guage = $this->getRequest()->getPost('option_grade');
//                    var_dump($option_guage);exit;
                    DB::table('topic_option')->where('opt_topicid','=',$top_id)->delete();
                    foreach ( $topic_option as $key => $val )
                    {
                        if( $val != '' )
                        {
                            $option_data['opt_topicid'] = $top_id;
                            $option_data['opt_desc'] = $val ;
                            $option_data['opt_grade'] = $option_guage[$key];
                            $option_data['opt_addtime'] = time();
                            DB::table('topic_option')->insert($option_data);
                        }
                    }
                } else if( $data['top_type'] == 5 ) {
                    //如果是跳转题
                    $topic_option = $this->getRequest()->getPost('option_guage');
                    $guage_id = $this->getRequest()->getPost('guage_id');
                    DB::table('topic_option')->where('opt_topicid','=',$top_id)->delete();
//                    var_dump($topic_option);exit;
                    foreach ( $topic_option as $key => $val )
                    {
                        $opt_guageid = $guage_id[$key];
                        if( $val != '' )
                        {
                            $option_data['opt_topicid'] = $top_id;
                            $option_data['opt_desc'] = $val;
                            $option_data['opt_guageid'] = $opt_guageid;
                            $option_data['opt_topic_type'] = 5;
                            $option_data['opt_addtime'] = time();
                            DB::table('topic_option')->insertGetId($option_data);
                        }
                    }
                }

                success('修改成功','Admin/Index/topic_list');

            } else {
                error('修改失败');
            }

        } else {

            //题目详细信息
            $where['top_id'] = $this->getRequest()->getQuery('top_id');
            $top_info = DB::table('topic')->where($where)->first();
            if( $top_info['top_type'] == 1 || $top_info['top_type'] == 2 || $top_info['top_type'] == 5  )
            {
                $whereOPT['opt_status'] = 1;
                $whereOPT['opt_topicid'] = $top_info['top_id'];
                $top_info['option'] = DB::table('topic_option')->where($whereOPT)->orderBy("opt_id",'asc')->get();
            }

            //问卷列表
            $whereQUE['qu_status'] = 1;
            $que_list = DB::table('questionnaire')->where($whereQUE)->get();

            //所属问卷大维度
            $whereDIM['dim_questionid'] = $top_info['top_questionnaireid'];
            $whereDIM['dim_status'] = 1;
            $whereDIM['dim_father'] = 0;

            $dim_list = DB::table('dimension')->where($whereDIM)->get();

            //第一个大维度的小维度
            $dim_second_list = DB::table('dimension')->where('dim_father','=',$top_info['top_dimension'])->get();

            $this->getView()->assign('top_info',$top_info);
            $this->getView()->assign('que_list',$que_list);
            $this->getView()->assign('dim_list',$dim_list);
            $this->getView()->assign('dim_second_list',$dim_second_list);

            $this->getView()->display('common/header.html');
            $this->getView()->display('index/topic_edit.html');
            $this->getView()->display('common/footer.html');

        }

    }
###################################题目模块 end################################
###################################配置模块 start################################
    public function setting_listAction()
    {
        //查出配置列表
        $where['set_status'] = 1;
        $setting_list = DB::table('setting')->where($where)->get();

        $this->getView()->assign('setting_list',$setting_list);

        $this->getView()->display('common/header.html');
        $this->getView()->display('index/setting_list.html');
        $this->getView()->display('common/footer.html');
    }

    public function setting_editAction()
    {
        //判断是否有表单提交
        if ($this->getRequest()->getPost())
        {
            //判断是否是图片
            if( isset( $_FILES['set_value_album'] ) ){

                $upath=time().abs(rand(1000000,9999999)).'.jpg';
                $cover=base64_decode($this->getRequest()->getPost('set_img_base64'));
                $pic=file_put_contents('uploads/'.$upath, $cover);

                if($pic){
                    $data['set_value'] = "uploads/".$upath;
                }
            } else {

                $data['set_value'] = $this->getRequest()->getPost('set_value');

            }

            $where['set_id'] = $this->getRequest()->getPost('set_id');

            $res = DB::table('setting')->where($where)->update($data);

            if( $res ){
                success('修改成功','Admin/Index/setting_list');
            } else {
                error('修改失败');
            }
        } else {

            //题目详细信息
            $where['set_id'] = $this->getRequest()->getQuery('set_id');
            $setting_info = DB::table('setting')->where($where)->first();

            $this->getView()->assign('setting_info',$setting_info);


            $this->getView()->display('common/header.html');
            $this->getView()->display('index/setting_edit.html');
            $this->getView()->display('common/footer.html');

        }
    }
###################################配置模块 end################################
###################################维度模块 start##############################
    //添加维度
    public function add_dimAction()
    {
        $data['dim_name'] = $this->getRequest()->getQuery('dim_name');
        $data['dim_questionid'] = $this->getRequest()->getQuery('dim_questionid');
        $data['dim_addtime'] = time();

        //如果是小维度
        if($this->getRequest()->getQuery('dim_father')){
            $data['dim_father'] = $this->getRequest()->getQuery('dim_father');
        }

        $res = DB::table('dimension')->insertGetId($data);

        if( $res )
        {
            echo $res;
        } else {
            echo 0;
        }
        exit;

    }

    //修改维度名称
    public function edit_dimAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery('dim_id');
        $data['dim_name'] = $this->getRequest()->getQuery('dim_name');

        $res = DB::table('dimension')->where($where)->update($data);

        if( $res )
        {
            echo 1;
        } else {
            echo 2;
        }
        exit;
    }

    //修改维度计分方式
    public function edit_dim_scoringAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery('dim_id');
        $data['dim_scoring'] = $this->getRequest()->getQuery('dim_scoring');

        $res = DB::table('dimension')->where($where)->update($data);

        if( $res )
        {
            echo 1;
        } else {
            echo 2;
        }
        exit;
    }

    //修改维度平均值
    public function edit_dim_meanAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery('dim_id');
        $data['dim_mean'] = $this->getRequest()->getQuery('dim_mean');

        $res = DB::table('dimension')->where($where)->update($data);

        if( $res )
        {
            echo 1;
        } else {
            echo 2;
        }
        exit;
    }

    //修改维度标准差
    public function edit_dim_deviationAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery('dim_id');
        $data['dim_deviation'] = $this->getRequest()->getQuery('dim_deviation');

        $res = DB::table('dimension')->where($where)->update($data);

        if( $res )
        {
            echo 1;
        } else {
            echo 2;
        }
        exit;
    }

    //根据问卷id查出所属的大维度
    public function que_dim_listAction()
    {
        $where['dim_questionid'] = $this->getRequest()->getQuery('qu_id');
        $where['dim_status'] = 1;
        $where['dim_father'] = 0;

        $dim_list = DB::table('dimension')->where($where)->get();
        echo json_encode($dim_list);
        exit;
    }

    //根据大维度id查出所属的小维度
    public function que_dim_second_listAction()
    {
        $where['dim_father'] = $this->getRequest()->getQuery('dim_id');
        $where['dim_status'] = 1;

        $dim_list = DB::table('dimension')->where($where)->get();
        echo json_encode($dim_list);
        exit;
    }

    //根据id删除维度
    public function del_dimAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery('dim_id');
        $qu_id = $this->getRequest()->getQuery('qu_id');
//        var_dump($qu_id);exit;
        $res = DB::table('dimension')->where($where)->delete();
        if($res)
        {
            success("删除成功","Admin/Index/questionnaire_edit",array( "qu_id" => $qu_id ));
        } else {
            error("删除失败，请重新尝试");
        }
        exit;
    }

    //将维度添加进问卷雷达维度表，然后修改该维度的是否显示在雷达图的值
    public function add_dim_radarAction()
    {
        $data['qrd_questionnaireid'] = $this->getRequest()->getQuery("qu_id");
        $data['qrd_dimensionid'] = $this->getRequest()->getQuery("dim_id");
        $data['qrd_order'] = $this->getRequest()->getQuery("order");

        $where['qrd_id'] = $this->getRequest()->getQuery("qrd_id",0);

        if( $where['qrd_id'] != 0 )
        {
            $res = DB::table('que_radar_dimension')->where($where)->update($data);
        } else {
            $data['qrd_addtime'] = time();
            $res = DB::table("que_radar_dimension")->insert($data);
        }

        if($res)
        {
            echo 1;
        } else {
            echo 2;
        }
        exit;

    }

    //修改actor属性的值
    public function edit_actorAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery("dim_id");
        $data['dim_actor'] = $this->getRequest()->getQuery("dim_actor");

        $res = DB::table("dimension")->where($where)->update($data);
        if($res)
        {
            echo 1;
        }
        else
        {
            echo 2;
        }
        exit;
    }

    //修改bad属性的值
    public function edit_badAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery("dim_id");
        $data['dim_bad'] = $this->getRequest()->getQuery("dim_bad");

        $res = DB::table("dimension")->where($where)->update($data);
        if($res)
        {
            echo 1;
        } else {
            echo 2;
        }
        exit;
    }

    //修改offer属性的值
    public function edit_offerAction()
    {
        $qu_id = $this->getRequest()->getQuery("qu_id");
        $where['dim_id'] = $this->getRequest()->getQuery("dim_id");
        $dim_offer = $_GET['dim_offer'];
        $data['dim_offer'] = str_replace("\n","<br>",$dim_offer);
        $res = DB::table("dimension")->where($where)->update($data);
        if($res)
        {
            success("修改成功","Admin/Index/questionnaire_edit",array( "qu_id" => $qu_id ));
        }
        else
        {
            error("修改失败，请重新尝试","Admin/Index/questionnaire_edit",array( "qu_id" => $qu_id ));
        }
        exit;
    }

    //修改coding属性的值
    public function edit_codingAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery("dim_id");
        $data['dim_coding'] = $this->getRequest()->getQuery("dim_coding");

        $res = DB::table("dimension")->where($where)->update($data);
        if($res)
        {
            echo 1;
        }
        else
        {
            echo 2;
        }
        exit;
    }
    //修改appellation属性的值
    public function edit_appellationAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery("dim_id");
        $data['dim_appellation'] = $this->getRequest()->getQuery("dim_appellation");

        $res = DB::table("dimension")->where($where)->update($data);
        if($res)
        {
            echo 1;
        }
        else
        {
            echo 2;
        }
        exit;
    }
    //修改维度图片属性的值
    public function edit_result_imgAction()
    {
        $where['dim_id'] = $this->getRequest()->getQuery("dim_id");

//        $base_64 = $this->getRequest()->getPost("dim_result_img");
//        $base64 = base64_decode( $base_64 );
//        $file_path = "uploads/".time().".png";
//        $res = file_put_contents($file_path,$base64);
        $base64_image_content = $this->getRequest()->getPost("dim_result_img");


        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
            $type = $result[2];

            $new_file = "uploads/";
            $new_file = $new_file.time().".{$type}";
            $res = file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)));

            if($res)
            {
                $data['dim_result_img'] = $new_file;
                $res = DB::table("dimension")->where($where)->update($data);
                if($res)
                {
                    echo 1;
                }
                else
                {
                    echo 2;
                }
            }
            else
            {
                echo 2;
            }
        }
        exit;
    }


###################################维度模块 end################################
###################################优惠码模块 start################################
    //优惠码列表
    public function code_listAction()
    {
        $page = $this->getRequest()->getQuery("page",1);
        $code_list = DB::table('code')
            ->paginate(20 , $page);

        $this->getView()->assign("code_list",$code_list);

        $this->getView()->display('common/header.html');
        $this->getView()->display('index/code_list.html');
        $this->getView()->display('common/footer.html');

    }

    //添加优惠码
    public function code_addAction()
    {
        //判断是否有表单提交
        if ($this->getRequest()->getPost())
        {

            $data['co_code'] = $this->getRequest()->getPost('co_code','');
            $data['co_addtime'] = time();

            $res = DB::table('code')->insert($data);
            if( $res )
            {
                success('添加成功','Admin/Index/code_list');
            }
            else
            {
                error('添加失败');
            }
        }
        else
        {

            $this->getView()->display('common/header.html');
            $this->getView()->display('index/code_add.html');
            $this->getView()->display('common/footer.html');

        }
    }

    //删除优惠码
    public function code_delAction()
    {
        $data['co_id'] = $this->getRequest()->getQuery('co_id');

        $res = DB::table('code')->where($data)->delete();

        if( $res )
        {
            success('删除成功','Admin/Index/code_list');
        }
        else
        {
            error('删除失败');
        }
    }
###################################优惠码模块 end################################
}

